package com.itic.ht.mem.order.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.code.persistence.model.Code;
import com.itic.appbase.applications.sys.code.persistence.model.CodeDetail;
import com.itic.appbase.applications.sys.code.service.CodeDetailService;
import com.itic.appbase.applications.sys.code.service.CodeService;
import com.itic.appbase.applications.sys.code.vo.CodeVo;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.ht.mem.order.persistence.model.MemberOrder;
import com.itic.ht.mem.order.persistence.model.MemberOrderExample;
import com.itic.ht.mem.order.service.MemberOrderService;


@Controller
@RequestMapping(value="/mem/order")
public class MemberOrderController
     extends BaseController {
    @Autowired
    private MemberOrderService orderService;
    
    @Autowired
    private CodeDetailService codeDetailService;

    @Autowired
    private CodeService codeService;

    //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
    private static final String MODEL_NAME_CHINA = "用户订单";//中文名
    private static final String MODEL_NAME＿ENGLISH = "order";//英文名
    private static final String PAGE_COMMON_PATH = "/mem/order/order";//通用路径
    private static final String COMMON_URL = "/mem/order/";//通用路径
    private static final String ID = "524f6177a6cf4a769d7bb37ccbb6a2dc";//codeID

    /*
     * 主页面
     */
    @RequestMapping(value="/init")
    public String init(Model model){
    	
        /*CodeVo codeVo = new CodeVo();
        codeVo = getCodeVo(ID, codeVo);
        System.out.println("**************************************************");
        System.out.println("codeVo.id======="+codeVo.getCode());
        System.out.println("**************************************************");
        model.addAttribute(codeVo);
    	return PAGE_COMMON_PATH+"Main";*/
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Main";
    }
    
    /**
     * 用户管理右侧列表页面
     * 
     * @param request
     * @param model
     * @return String
     */
    /*@RequestMapping(value = "/initOrder")
    public String initOrder() {
        //return "sys/user/userList";
        return PAGE_COMMON_PATH+"List";
    }*/
    
    /*
     * 添加页面
     */
    @RequestMapping(value="/initAdd")
    public String initAdd(Model model){
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Add";
    }
    
    /*
     * 编辑页面
     */
    @RequestMapping(value="/initEdit")
    public String initEdit(Integer id , Model model){
    	if( null != id ) {
    		MemberOrder temp = orderService.get(id);
    		model.addAttribute("editData",iticJsonMapper.toJson(temp));
    		model.addAttribute("name_china", MODEL_NAME_CHINA);
        	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
        	model.addAttribute("common_url", COMMON_URL);
    	}else {
            throw new ParameterErrorException();
        }
    	return PAGE_COMMON_PATH+"Edit";
    }
    
    /*
     * 查看页面
     */
    @RequestMapping(value="/initView")
    public String initView(Integer id , Model model){
    	if( null != id ) {
    		
    		MemberOrder temp  = orderService.get(id);
    		
    		model.addAttribute("viewEdit",iticJsonMapper.toJson(temp));
    		model.addAttribute("name_china", MODEL_NAME_CHINA);
        	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
        	model.addAttribute("common_url", COMMON_URL);
    	}else {
            throw new ParameterErrorException();
        }
    	
    	return PAGE_COMMON_PATH+"View";
    }
    
    /**
     * 
     * @param goldType
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(MemberOrder temp, HttpServletRequest request){
        // 声明返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        // 参数处理
        MemberOrderExample example = new MemberOrderExample();
        // 按创建时间倒序
        example.setOrderByClause("ctime desc");
        if(StringHelper.isNotBlank(temp.getAccount())){
        	example.createCriteria().andAccountLike("%"+temp.getAccount()+"%");
        }
        
        if(StringHelper.isNotBlank(temp.getOrderType())){
        	example.createCriteria().andOrderTypeEqualTo(temp.getOrderType());
        }
        
        if(StringHelper.isNotBlank(temp.getOrderNum())){
        	example.createCriteria().andOrderNumEqualTo(temp.getOrderNum());
        }
        
        //example.createCriteria().andOrderNumEqualTo(temp.getOrderNum());
        
       
        // 封装分页参数
        example = this.handlePageQuery(request, example);
        
        
        // 获取信息总条数
        int total = orderService.count(example);
        
        // 获取数据
        List<MemberOrder> list = orderService.list(example);
        
        // 数据返回处理
        resultMap.put(IticConst.TOTAL_RECORDS, total);
        resultMap.put(IticConst.DISPLAY_RECORDS, total);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;

    }
    
    /**
     * 添加模板管理数据
     * @param android
     * @return
     */
    @RequestMapping(value="/add")
    @ResponseBody
    public Map<String,String> add(MemberOrder temp){
    	
    	 Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         temp.setCtime(new Date());
         temp.setMtime(new Date());
         //商品数量默认为1
         temp.setItemCount(1);
         result = orderService.add(temp);
         
         resultMap.put(IticConst.RESULT, result);
         return resultMap;
    }
    /**
     * 编辑保存
     * @param templet
     * @return
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public Map<String,String> edit( MemberOrder temp){
    	
    	 Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         result = orderService.edit(temp);
         resultMap.put(IticConst.RESULT, result);
         return resultMap;
    }
    
    /**
     * 删除
     * 
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, String> del(Integer id) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String result = ResponseCode.SERVER_ERROR.getCode();

        if( null != id ){
        	MemberOrder temp = new MemberOrder();
        	temp.setId(id);
        	temp.setStatus("-1");
            result = orderService.delete(temp);
            //result = addressService.deltrue(id);
        }

        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }
    
    /**
     * 异步刷新左侧树数据
     * 
     * @param parentId
     * @param request
     * @return
     */
    @RequestMapping(value = "/asyncDataTree")
    @ResponseBody
    public List<CodeDetail> asyncDataTree(String parentId) {
        // 获取数据
        return codeDetailService.selectCodeDetail(parentId);// 获取代码详情信息
    }
    
    /**
     * 左侧树数据展示
     * 
     * @param parentId
     * @param request
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    @RequestMapping(value = "/dataTree")
    @ResponseBody
    public Map<String, Object> dataTree(String parentId) {
        // 声明返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 获取数据
        List<CodeDetail> codeList = null;
        Code code = codeService.get(parentId);// 获取代码表原数据信息
        if (null != code) {
            CodeDetail codeDetail = new CodeDetail();
            // 将代码表中的code，做为详情表代码树的根节点
            codeDetail = (CodeDetail) ValueHelper.model2vo(code, codeDetail);
            if (null != codeDetail) {
                codeList = codeDetailService.selectCodeDetail(parentId);// 获取代码详情信息
                codeList.add(codeDetail);// 添加根节点
            }
        }
        // 数据返回处理
        resultMap.put(IticConst.DATA, codeList);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        return resultMap;
    }
    
    /**
     * 代码表或者代码详情表转vo
     * 
     * @param id
     * @param codeVo
     * @return
     */
    private CodeVo getCodeVo(String id, CodeVo codeVo) {
        Code code = codeService.get(id);
        if (null == code) {
            CodeDetail codeDetail = codeDetailService.get(id);
            if (null != codeDetail) {
                codeVo.setCode(codeDetail.getCode());// 子类代码表父类代码
                codeVo.setType(codeDetail.getType());// 子类代码表父类类型
                codeVo.setId(codeDetail.getId());// 子类代码表父类id
                codeVo.setName(codeDetail.getName());// 子类代码表父类名称
            }
        } else {
            codeVo.setCode(code.getCode());// 子类代码表父类代码
            codeVo.setType(code.getCode());// 子类代码表父类类型
            codeVo.setId(code.getId());// 子类代码表父类id
            codeVo.setName(code.getName());// 子类代码表父类名称
        }
        return codeVo;
    }
    
}